/*----------------------------------------------------------------------
  - File name      : STC8Ax_ADC.c
  - Author         : Quark Team
  - Update date    : 2020-08-30                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: ADC module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_ADC.h"
#include "intrins.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
/* None */
/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: ADC peripheral init function             |
| @param      : const ADC_InitType *ADCx                 |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE ADC_Init(const ADC_InitType *ADCx)
{
    if(ADCx -> Speed <= 0x0F) 
    {
        EAXFR_ENABLE();
        ADCTIM = 0x3F;
        EAXFR_DISABLE();
        ADC_CONTR = (ADC_CONTR & 0x70)|(ADCx -> Power << 7)|(ADCx -> Road);
        _nop_();_nop_();_nop_();
        ADCCFG = (ADCCFG & 0xF0) | (ADCx -> Speed);	
        ADCCFG = (ADCCFG & 0xDF) | (ADCx -> Align << 5);	
        ADC_CONTR = (ADC_CONTR & 0xBF) | (ADCx -> Run << 6);
        return FSC_SUCCESS;
    }
    else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: ADC get sample function                  |
| @param      : ADCRoad_Type Road,                       |
|               ADCAcc_Type Acc,                         |
|               uint16 *Value                            |
| @return     : Data                                     |
--------------------------------------------------------*/

uint16 ADC_Get_Sample(ADCRoad_Type Road, ADCAcc_Type Acc)
{	
    uint16 Value;
    ADC_RES = 0;
    ADC_RESL = 0;
    ADC_CONTR = (ADC_CONTR & 0xF0) | (Road);
	while (!(ADC_CONTR & 0x20));  //Query ADC completion flag
	ADC_CONTR &= 0xDF;  //Completion mark
	Value = (uint16)(ADC_RES << 8) | ADC_RESL; 
	Value >>= Acc; //Selection accuracy
    ADC_CONTR |= 0x40; //Restart
	return Value;
}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



